#include <main.h>
#define LCD_ENABLE_PIN PIN_D2
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7

#include <lcd.c>

// define function/subroutines
void analog(void);			
void SW_A(void);
void SW_B(void);
void SW_C(void);
void SW_D(void);

// declare global variables
unsigned int adc;
unsigned int switches;
int disp = 0;
unsigned int last_button_state;
unsigned int lightsA[16]={128,64,32,16,8,4,2,1,1,2,4,8,16,32,64,128};

//start of the main loop
void main()
{
	setup_adc_ports(sAN0);							// initialise adc channel
	setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);	// setup adc operation

	lcd_init();										// initialise the LCD

	while(TRUE)
	{
		analog();					//call read the analog function

		switches=input_a();			//read value of port A
		switches=switches & 0x3c;	//mask of unwanted bits 00111100
		switches/=2;				//dived by 2

		switch(switches)
		{
			case 2: 				//switch in first position
			SW_A();					//call function SW_A
			disp=0;					//reset disp variable
			break;

			case 4:					//switch in secound position  
			SW_B();					//call function SW_B
			disp=0;					//reset disp variable 
			break;

			case 8:					//switch in third position
			SW_C();					//call function SW_C
			disp=0;					//reset disp variable
			break;

			case 16:				//switch in fourth position
			disp = disp+1; 			//add 1 to the disp variable
			SW_D();					//call function SW_D
			break;
			default:  break;
		}																								
	}
}

//read the adc
void analog(void)
{
	set_adc_channel(0);		// read adc channel 0
	delay_us(30);			// wait 30uS
	adc = read_adc();		// store value in variable ADC
}

void SW_A(void)
{
	printf(lcd_putc,"\f"); 				//clear the LCD display
	lcd_gotoxy(7,1);					//goto line 1 position 7 on the LCD display
	printf(lcd_putc,"ADC= %3u", adc);	//print the ADC value to the LCD display

	if (adc <= 75)
    {
		output_low(PIN_C0);							//set pin C0 low
		output_high(PIN_C1);						//set pin C1 high
		lcd_gotoxy(2,2);							//got to line 2 position 2
        printf(lcd_putc,"Spin Anticlockwise");		//print "Spin Anticlockwise" to the LCD display
        delay_ms(100);								//100ms delay
    }
      
   if (adc >= 175)
    {
		output_low(PIN_C1);						//set pin C1 low
		output_high(PIN_C0);					//set pin C0 high
		lcd_gotoxy(4,2);						//go to line 2 position 4
        printf(lcd_putc,"Spin Clockwise");		//print "spin clockwise" to the LCD display
        delay_ms(100);							//100ms delay
    }
      
   if((adc >=76) & (adc <= 174))
    {
		output_low(PIN_C0);					//set pin C0 low
		output_low(PIN_C1);					//set pin C1 low
		lcd_gotoxy(6,2); 					//centre on line 2
        printf(lcd_putc,"Don't Spin"); 		//print "Don't Spin" to the LCD display
        delay_ms(100);						//100ms delay
    }

	//used to stop motors when the switch changes positions
	output_low(PIN_C0);						//set pin C0 low
	output_low(PIN_C1);						//set pin C1 low
	printf(lcd_putc,"\f");					//clear the LCD display
}

void SW_B(void)
{
	output_b(adc & 0xFF);  				//displays adc in bitwise on the 8 leds 
	lcd_gotoxy(7,1); 					//go to line 1 postion 7 on the LCD display
    printf(lcd_putc,"ADC= %3u", adc); 	//print the ADC value to the LCD display
	printf("ADC= %3u \r\n", adc);		//print the ADC value to the virtual terminal
	delay_ms(500);						//500ms delay
	printf(lcd_putc,"\f");				//clear the LCD display 
	output_b(0x00);						//set all leds to off
}

void SW_C(void)
{
	int current_button_state = input(pin_a1);		//read the curret button state
	if(current_button_state != last_button_state)	//cheks if the current button state is not the same as its previous state
	{
		if (current_button_state)
		{
			printf("Button pressed \r\n");			//print to the virutal terminal
			output_low(PIN_C1);						//set pin C1 low for clockwise motor movement
			output_high(PIN_C0);					//set pin C0 high for clockwise motor movement
			lcd_gotoxy(24,1);						//go to center of line 3 on the LCD display
			printf(lcd_putc,"Button pressed ");		//print to the LCD display
		}
		else
		{
			printf("Button released \r\n");			//print to the virtual terminal
			output_low(PIN_C1);						//set pin C1 low to turn the motor off
			output_low(PIN_C0);						//set pin C1 low to turn the motor off
			lcd_gotoxy(24,1);						//go to center of line 3 on the LCD display 
			printf(lcd_putc,"Button released ");	//print to the LCD display
		}
		last_button_state = current_button_state;	//update last button state
	}
}

//kinght rider led function
void SW_D()
{
	printf(lcd_putc,"\f"); 						//clear the LCD display
	lcd_gotoxy(27,2);							//got to center of line 4 on the LCD display
	printf(lcd_putc,"Kitt mode");				//print to LCD display
	if(disp == 1) printf("Kitt mode \r\n");		//if disp is 1 print to virtual termial
	unsigned int q; 							//loop counter variable

	//sequence for knight rider led's
	for(q=0;q<16;q++)
	{
		output_b(lightsA[q]);	//turn the speficed led on
		delay_ms(200);			//delay of 200ms
	}
	output_b(0x00);				//set all leds to off
	printf(lcd_putc,"\f");		//clear the LCD display
}